package work;

import java.util.Random;
import java.util.Scanner;

public class AppleGame2 {
    // 水果名称数组
    private static final String[] FRUITS = {"苹果", "木瓜", "西瓜", "香蕉", "橙子", "葡萄"};
    // 奖励系数数组
    private static final int[] REWARDS = {2, 5, 10, 20, 50, 100};
    // 各水果对应的概率区间上限
    private static final int[] PROBABILITIES = {50, 75, 88, 95, 99, 100};

    public static void main(String[] args) {
        test();
    }

    public static void test() {
        Scanner sc = new Scanner(System.in);
        int money = 10;

        while (true) {
            menu(money);
            int storage = money;

            // 获取用户选择的水果
            String choice = getValidFruitChoice(sc);
            if ("Q".equals(choice)) {
                System.out.println("退出游戏");
                break;
            }

            // 获取用户下注的金币数
            int betting = getValidBetting(sc, money);

            System.out.println("幸运机开始运转----------Good Luck");
            int num = getRandom();
            String lucky = FRUITS[num - 1];
            System.out.println("-----" + lucky + "-----");

            money -= betting;
            if (choice.equals(lucky)) {
                money += REWARDS[num - 1] * betting;
                System.out.println("恭喜您,猜对了!,您赢了" + (money - storage + betting));
            } else {
                System.out.println("很遗憾,您猜错了,您损失了" + betting + "金币");
            }

            if (money == 0) {
                System.out.println("您的筹码已经完了,退出游戏");
                break;
            }
        }
        sc.close();
    }

    // 获取用户输入的有效水果选择
    private static String getValidFruitChoice(Scanner sc) {
        while (true) {
            System.out.println("请选择您选中的水果:");
            String choice = sc.next();
            if ("Q".equals(choice)) {
                return choice;
            }
            for (String fruit : FRUITS) {
                if (fruit.equals(choice)) {
                    return choice;
                }
            }
            System.out.println("无效的水果选择，请重新输入。");
        }
    }

    // 获取用户输入的有效下注金币数
    private static int getValidBetting(Scanner sc, int money) {
        while (true) {
            try {
                System.out.println("请选择您要下注的金币数(最多" + money + "金币)");
                int betting = sc.nextInt();
                if (betting > money || betting < 0) {
                    System.out.println("您下注的金币数不合理,请重新下注:");
                } else {
                    return betting;
                }
            } catch (Exception e) {
                System.out.println("输入无效，请输入一个有效的数字。");
                sc.nextLine(); // 清除无效输入
            }
        }
    }

    // 根据概率生成随机数
    public static int getRandom() {
        Random random = new Random();
        int num = random.nextInt(100) + 1;
        for (int i = 0; i < PROBABILITIES.length; i++) {
            if (num <= PROBABILITIES[i]) {
                return i + 1;
            }
        }
        return -1; // 理论上不会执行到这里
    }

    public static void menu(int money) {
        System.out.println("澳门新葡京娱乐城8848,搏一搏,单车变摩托!");
        System.out.println("您当前的金币总计:" + "$" + money);
        System.out.println("欢迎进入游戏,新的一轮游戏马上开始!");
        System.out.println("猜中的物品和对应的奖励系数如下:");
        for (int i = 0; i < FRUITS.length; i++) {
            System.out.println(FRUITS[i] + "-----------" + REWARDS[i]);
        }
        System.out.println("游戏结束,请按Q");
    }
}